import 'dart:async';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../resources/kq_theme_colors.dart';

class KqCountDownButton extends StatefulWidget {
  /// 倒计时数值
  final int countDownValue;

  /// 是否圆形波纹
  final double radius;

  /// 水平左右padding，默认0
  final double horizontalPadding;

  /// 垂直左右padding，默认0
  final double verticalPadding;

  /// 背景颜色，默认透明，注意：设置padding后，会带上padding范围
  final Color backgroundColor;

  /// 边框颜色，默认无边框
  final Color borderColor;

  /// 边框宽度
  final double borderWidth;

  /// 点击后的回调
  final Function(CountDownButtonState state)? onTap;

  /// 文本组件
  final Widget Function(CountDownButtonState state, int value)? textBuilder;

  const KqCountDownButton({
    super.key,
    required this.countDownValue,
    this.onTap,
    this.radius = 20,
    this.horizontalPadding = 20,
    this.verticalPadding = 6,
    this.backgroundColor = KqThemeColors.bgWhite,
    this.borderColor = KqThemeColors.bgD8,
    this.borderWidth = 1,
    this.textBuilder,
  });

  @override
  State<StatefulWidget> createState() => CountDownButtonState();
}

class CountDownButtonState extends State<KqCountDownButton> {
  /// 当前进度值
  int _value = 0;

  /// 按钮是否不可用
  bool _disabled = false;

  /// 是否第一次点击
  bool _isFirstClick = true;

  /// 计时器
  Timer? timer;

  @override
  void initState() {
    super.initState();
  }

  ///开始倒计时
  void startTimer() {
    _isFirstClick = false;
    int count = 0;
    //计时器，每1秒执行一次
    const period = Duration(seconds: 1);
    timer = Timer.periodic(period, (timer) {
      count++;
      num max = widget.countDownValue;
      //计时器结束条件
      if (widget.countDownValue == 0 || count >= max) {
        timer.cancel();
        if (mounted) {
          setState(() {
            _value = 0;
            _disabled = false;
          });
        }
      } else {
        if (mounted) {
          setState(() {
            _value = widget.countDownValue - count;
            _disabled = true;
          });
        }
      }
    });
  }

  ///倒计时是否已结束
  bool isCountdownFinish() {
    return !_disabled;
  }

  ///是否第一次点击
  bool isFirstChick() {
    return _isFirstClick;
  }

  @override
  void dispose() {
    //退出时关闭计时器防止内存泄露
    timer?.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return KqInkWell(
      radius: widget.radius,
      horizontalPadding: widget.horizontalPadding,
      verticalPadding: widget.verticalPadding,
      borderWidth: widget.borderWidth,
      borderColor: widget.borderColor,
      backgroundColor: widget.backgroundColor,
      onTap: () {
        if (!_disabled) {
          widget.onTap?.call(this);
        }
      },
      enableRipple: !_disabled,
      child: (widget.textBuilder?.call(this, _value)) ??
          Text(
            _value.toString(),
            style: TextStyle(
              fontSize: 12.sp,
              color: KqThemeColors.text26,
            ),
          ),
    );
  }
}
